﻿<cfscript>
/** 
* @hint "bookBase - BookSchema"
*/ 

component 
	extends="MachII.framework.Listener" 
	displayname="acadmicManager.listeners.senate.bookPlanListener" 
	output=false 
	depends="sequenceCourseBook"
	{
	
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 

	/**
	* @hint "删除教材计划"
	* 
	* @event "MachII Event Object"
	*/
	public void function deletePlan( required MachII.framework.Event event ) output=false {
		
		var courseBook = "";
		var course = "";
		
		var courseBookId = event.getArg("CourseBookID");
		var courseId = event.getArg("CourseID");
		
		var sql = ""; 
		var queryObj = ""; 

		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "DELETE FROM t_course_book WHERE cbid = :courseBookID"; 
			
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="courseBookID", value=courseBookId, cfsqltype="cf_sql_varchar" ); 
			
			queryObj.execute( sql=sql); 
			
			
			sql = "SELECT institute_id FROM t_course WHERE cid = :courseId "; 
			
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" ); 
			
			rs_course = queryObj.execute( sql=sql).getResult();; 
			
			event.setArg("DepID", rs_course.institute_id );
		}
		
	}
	
	/**
	* @hint "增加教材计划数量 联系人"
	* 
	* @event "MachII Event Object"
	*/
	public void function editPlan( required MachII.framework.Event event ) output=false {

		var courseBook = "";
		
		var courseBookId = event.getArg("CourseBookID");
		
		var sql = ""; 
		queryObj = ""; 
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "UPDATE t_course_book SET 
						 plan_sum = :planCounts, 
						 plan_student = :forStudent, 
						 plan_teacher = :forTeacher, 
						 contract = :contract, 
						 telephone = :telephone 
					WHERE 
						 cbid = :courseBookId";
			
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="courseBookId", value=courseBookId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="planCounts", value=event.getArg('ForStudent', 0) + event.getArg('ForTeacher', 0), cfsqltype="cf_sql_integer" );
			queryObj.addParam( name="forStudent", value=event.getArg('ForStudent', 0), cfsqltype="cf_sql_integer" );
			queryObj.addParam( name="forTeacher", value=event.getArg('ForTeacher', 0), cfsqltype="cf_sql_integer" );
			queryObj.addParam( name="contract", value=event.getArg('Contract', '-'), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="telephone", value=event.getArg('Telephone', '-'), cfsqltype="cf_sql_varchar" );
			
			queryObj.execute( sql=sql );
			
			sql = "SELECT 
						 t_course.institute_id 
					FROM  
						 t_course_book 
						 INNER JOIN t_course ON t_course.cid = t_course_book.cid 
					WHERE 
						 t_course_book.cbid = :courseBookId "; 
			
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="courseBookId", value=courseBookId, cfsqltype="cf_sql_varchar" );
			
			rs_courseBook = queryObj.execute( sql=sql ).getResult(); 

			event.setArg("DepID", rs_courseBook.institute_id );
			event.setArg("CBID", courseBookId );
			
		}
	}
	
	/**
	* @hint "增加教材计划信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function addPlan( required MachII.framework.Event event ) output=false {
		
		var sequenceCourseBook = getsequenceCourseBook();
		
		var term = "";
		var course = "";
		var book = "";
		var courseBook = "";

		var termId = event.getArg("TermID");
		var courseId = event.getArg("CourseID");
		var bookId = event.getArg("BookID");
		
		var bookIdx = 0;
		var courseBookId = 0;
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "	SELECT
						i.institute_id,i.institute_name
					FROM
						t_course c INNER JOIN t_institute i ON i.institute_id = c.institute_id
					WHERE
						c.cid = :courseId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "courseId", value = courseId, cfsqltype = "cf_sql_varchar");
			rs_courseEntity = queryObj.execute(sql = sql).getResult();

			sql = "	SELECT
						cb.cbid
					FROM
						t_course_book cb
					WHERE
						cb.term_id = :termId AND cb.cid = :courseId AND cb.bid = :bookId ";

			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "termId", value = termId, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "courseId", value = courseId, cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "bookId", value = bookId, cfsqltype = "cf_sql_varchar");
			rs_courseBookEntity = queryObj.execute(sql = sql).getResult();
			
			if ( rs_courseBookEntity.recordCount eq 0 ) {

				sequenceCourseBook.setTermID(termId);
				sequenceCourseBook.setCourseID(courseId);
				bookIdx = sequenceCourseBook.getBookIDX();
				sequenceCourseBook.setBookIndex(bookIdx);
				courseBookId=sequenceCourseBook.getID();

				sql = "	INSERT INTO t_course_book(
							cbid,
							term_id,
							cid,
							bid,
							prise,
							discount,
							charge,
							plan_sum,
							bidx,
							plan_student,
							plan_teacher,
							contract,
							telephone
						)VALUES(
							:courseBookId,
							:termId,
							:courseId,
							:bookId,
							:prise,
							:disCount,
							:charge,
							:planSum,
							:bookIndex,
							:planStudent,
							:planTeacher,
							:contract,
							:telephone 
						)";
				
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name = "courseBookId", value = courseBookId, cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "termId", value = termId, cfsqltype = "cf_sql_char");
				queryObj.addParam(name = "courseId", value = courseId, cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "bookId", value = bookId, cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "prise", value = event.getArg('Prise', 0), cfsqltype = "cf_sql_decimal", scale = 2);
				queryObj.addParam(name = "disCount", value = event.getArg('Discount', 1.00), cfsqltype = "cf_sql_decimal", scale = 2);
				queryObj.addParam(name = "charge", value = event.getArg('Charge', 0), cfsqltype = "cf_sql_decimal", scale = 2);
				queryObj.addParam(name = "planSum", value = event.getArg('ForStudent', 0) + event.getArg('ForTeacher', 0), cfsqltype = "cf_sql_integer");
				queryObj.addParam(name = "bookIndex", value = bookIdx, cfsqltype = "cf_sql_char");
				queryObj.addParam(name = "planStudent", value = event.getArg('ForStudent', 0), cfsqltype = "cf_sql_integer");
				queryObj.addParam(name = "planTeacher", value = event.getArg('ForTeacher', 0), cfsqltype = "cf_sql_integer");
				queryObj.addParam(name = "contract", value = event.getArg('Contract', '-'), cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "telephone", value = event.getArg('Telephone', '-'), cfsqltype = "cf_sql_varchar");
				queryObj.execute(sql = sql);
				
			}else{
				courseBookId = rs_courseBookEntity.cbid;
				
				sql = "	UPDATE
							t_course_book t
						SET
							t.prise = :prise,
							t.discount = :disCount,
							t.charge = :charge,
							t.plan_sum = :planSum,
							t.plan_student = :planStudent,
							t.plan_teacher = :planTeacher,
							t.contract = :contract,
							t.telephone = :telephone 
						WHERE
							t.cbid = :courseBookId ";
							
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name = "courseBookId", value = courseBookId, cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "prise", value = event.getArg('Prise', 0), cfsqltype = "cf_sql_decimal", scale = 2);
				queryObj.addParam(name = "disCount", value = event.getArg('Discount', 1.00), cfsqltype = "cf_sql_decimal", scale = 2);
				queryObj.addParam(name = "charge", value = event.getArg('Charge', 0), cfsqltype = "cf_sql_decimal", scale = 2);
				queryObj.addParam(name = "planSum", value = event.getArg('ForStudent', 0) + event.getArg('ForTeacher', 0), cfsqltype = "cf_sql_integer");
				queryObj.addParam(name = "planStudent", value = event.getArg('ForStudent', 0), cfsqltype = "cf_sql_integer");
				queryObj.addParam(name = "planTeacher", value = event.getArg('ForTeacher', 0), cfsqltype = "cf_sql_integer");
				queryObj.addParam(name = "contract", value = event.getArg('Contract', '-'), cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "telephone", value = event.getArg('Telephone', '-'), cfsqltype = "cf_sql_varchar");
				queryObj.execute(sql = sql);
				
			}
			
			event.setArg("DepID", rs_courseEntity.institute_id );
			event.setArg("CBID", courseBookId );
			event.setArg("CID", courseId );
			event.setArg("BID", bookId );
			
		}
		
	}

}

</cfscript>